<!--
 * @Description: 邀请好友管理
 * @Author: liyujie
 * @Date: 2021/10/16
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/10/16
-->
<template>
    <PageLayout title="邀请管理">
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                type="index"
                align="center"
                width="80"
            />
            <el-table-column
                prop="type"
                label="拉新类型"
            >
                <template slot-scope="{row}">
                    {{ row.type | typeFilter }}
                </template>
            </el-table-column>
            <el-table-column
                label="上架状态"
                align="center"
            >
                <template slot-scope="{row}">
                    <el-switch
                        v-model="row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="setStatus(row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
            >
                <template slot-scope="{row}">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            @click="toConfigPage(row)"
                        >
                            全局配置
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            v-if="row.type === 104"
                            @click="handleRoute('invitePkActivityList')"
                        >
                            进入活动
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </PageLayout>
</template>

<script>
export default {
    name: 'InviteManage',
    data() {
        return {
            tableData: [],
            tableLoading: false,
            authResult: {},
        };
    },
    filters: {
        typeFilter(val) {
            switch (val) {
                case 101:
                    return '邀请有礼';
                case 104:
                    return '邀请PK赛';
                default:
                    return '--';
            }
        },
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '邀请好友'}, {title: '邀请管理'}]);
            this.getTableData();
            this.authResult = this.$route.meta.authResult;
        },

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;

            this.$post('/student/task_stuagent_extra_student_invite_activity%5Cconfig%5Cget_invite_activity_manage_list', {}, res => {
                if (res.code === 1) {
                    this.tableData = res.data;
                } else {
                    this.$notify({
                        title: '提示',
                        type: 'warning',
                        message: res.msg
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         * @param auth_name
         */
        handleRoute(routeName, query, auth_name) {
            if(auth_name && !this.authResult[auth_name]) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 设置上下架状态
         * @param row
         */
        setStatus(row) {
            let params = {
                status: row.status,
                type: row.type
            };

            this.$post('/student/task_stuagent_extra_student_invite_activity%5Cconfig%5Cset_invite_activity_status', params, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '提示',
                        type: 'success',
                        message: '操作成功'
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        type: 'warning',
                        message: resp.msg
                    });
                }
                this.getTableData();
            });
        },

        /**
         * 进入配置页面
         * @param row
         */
        toConfigPage(row) {
            if(row.type === 101) {
                this.handleRoute('inviteFriendsConfig');
            }

            if(row.type === 104) {
                this.handleRoute('invitePkActivityConfig', {}, 'auth_config_pk');
            }
        }
    }
};
</script>
